<?php
 defined("BASEDIR") or die();
 
 class Application_Controller_Zapisy extends Application_Controller_Default
 {
	public function defaultAction()
	{
		$user = Application_Router_Standard_User::getInstance($this->request);
		if($user->level > 2)
		{
			$this->request->action = "notallowed";
			return;
		}
		$this->request->action = $this->request->controller;
		
		$db = Application_Model_DataBase::getInstance();
		
		$db->getTable("Rodzaj_zabiegu");
		$db->execute();
		while($u = $db->getResult())
			$zabiegi[] =$u;
		$this->view->assign("zabiegiData", $zabiegi);
	}
        public function wyslijAction()
        {
            $user = Application_Router_Standard_User::getInstance($this->request);
		if($user->level > 1)
			$this->request->action = "notallowed";
		else
		{
		#	$this->modInfo['title'] = "Załóż konto";
			// weryfikacja danych
                        $ses = Application_Model_SessionControl::getInstance();
                        $username=$ses->un;
			$itemarr = array();
			$err = array();
                        $db = Application_Model_DataBase::getInstance();
                        $db->getTable("Zabiegi");
                        $db->execute();
                        while($query=$db->getResult())
                        {
                            $dates[]=$query["data"];
                            $starts[]=$query["start"];
                            print_r($starts);
                            print("<br><br>");
                        }
                        #   $pids[]=$query["pid"];
                        #   
                            #$db->getTable("Zabiegi");
                        $datass=$this->request->data;
                            $a=  explode(" ", $datass);
                            switch ($a[1]){
    case "Styczeń":
        $a[1]="01";
        break;
    case "Luty":
        $a[1]="02";
        break;
    case "Marzec":
        $a[1]="03";
        break;
     case "Kwiecień":
        $a[1]="04";
        break;
    case "Maj":
        $a[1]="05";
        break;
    case "Czerwiec":
        $a[1]="06";
        break;
     case "Lipiec":
        $a[1]="07";
        break;
    case "Sierpień":
        $a[1]="08";
        break;
    case "Wrzesień":
        $a[1]="09";
        break;
     case "Październik":
        $a[1]="10";
        break;
    case "Listopad":
        $a[1]="11";
        break;
 case "Grudzień":
        $a[1]="12";
        break;
}                           
                            $help=$a[0];
                            $a[0]=$a[2];
                            $a[2]=$help;
                            $b=implode("-", $a);
                            print($b);
                            $time=$this->request->godzina.":00";
                            $ex_time=explode(":", $time);
                            $endint=intval($ex_time[0])+1;
                            if(intval($ex_time[0])<10)
                            $ex_time[0]="0".$ex_time[0];
                            else
                              $ex_time[0]=$ex_time[0];  
                            if($endint<10)
                            $end = "0".(string)$endint;
                        else 
                            $end = (string)$endint;
                        
                            print("<br>");
                            print($end);
                            print("<br>");
                            print($ex_time[0]);
                            $time_start=implode(":", $ex_time);
                            print("<br>");
                            print($time_start);
                            $ex_time[0]=$end;
                            
                            $time_stop=implode(":", $ex_time);
                            print("<br>");
                            print($time_stop);
                            $db->query("SELECT zid from Zabiegi where data='".$b."' and start='".$time_start."'");
                            $db->execute();
                            $querys=$db->getResult();
                            print_r($querys);
                            #else $err[] = "PESEL musi się składać z 11 znaków liczbowych";
                      #  }
                        # print_r($dates);
                        # print_r($starts);
                        # print($this->request->zabieg);
			/*if($this->request->reg_zabieg != "" && strlen($this->request->reg_pesel) == 11 && preg_match("/[0-9]/",$this->request->reg_pesel)) $itemarr['reg_pesel'] = $this->request->reg_pesel;
			else $err[] = "PESEL musi się składać z 11 znaków liczbowych";
			if($this->request->reg_email != "" && preg_match("/[a-zA-Z0-9_\.]@[0-9a-zA-Z_]{1,}.[a-z]/", $this->request->reg_email)) $itemarr['reg_email'] = $this->request->reg_email;
			else $err[] = "Adres email niepoprawny";
			if($this->request->reg_password1 != "" && $this->request->reg_password2 != "" && $this->request->reg_password1 == $this->request->reg_password2) $itemarr[] = $this->request->reg_password1;
			else $err[] = "Podane hasła nie zgadzają się";
			if(strlen($this->request->reg_password1) < 7) $err[] = "Hasło musi mieć co najmniej 8 znaków";
			if($this->request->reg_first_name != "") $itemarr['reg_first_name'] = $this->request->reg_first_name;
			else $err[] = "Nie podano imienia";
			if($this->request->reg_last_name != "") $itemarr['reg_last_name'] = $this->request->reg_last_name;
			else $err[] = "Nie podano nazwiska";
			if($this->request->reg_tel != "") $itemarr['reg_tel'] = $this->request->reg_tel;
			else $err[] = "Nie podano numeru telefonu";
			
			if($this->request->reg_accept == "on") $itemarr[] = $this->request_reg_accept;
			else $err[] = "Postanowienia regulaminu nie zostały zaakceptowane";
			*/if(empty($querys)) {
                            
                            $itemarr['data'] = $b;
                            $itemarr['start'] = $time_start;
                            $itemarr['stop'] = $time_stop;
                            $itemarr['tid'] = $this->request->zabieg;
                            $itemarr['pesel'] = $username;
                            $itemarr['platnosc_id'] = $this->request->platnosc;
                            
			}
                        else
                            $err="bledy okropne";;
                            if(empty($err) && !empty($itemarr))
			{
				
				$db = Application_Model_DataBase::getInstance();
				#$ip = (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
				
                                $db->getTable("Zabiegi", "INSERT");
				$db->setFields("tid, start, koniec, data");
				$values = array($itemarr['tid'], $itemarr['start'], $itemarr['stop'], $itemarr['data']);
				$db->setValues(implode(",", $values));
				$num = $db->execute();
                                
				if($db->errno) // duplicate entry
				{          
					$this->view->assign("warning", "DB errors while executing insert");
					$this->view->assign("dump", $itemarr);
                                        print($itemarr);
                                        print"shit happend";
				}
				if($num == 1)
				{         
					$this->view->assign("complete", $num);
					
				}
                                print($b);
                                $db = Application_Model_DataBase::getInstance();
                                $db->query("SELECT zid from Zabiegi where start='".$time_start."' and data ='".$b."'");
				$numss=$db->execute();
                                #print($numss);//1
                                $zids=$db->getResult();
                                print_r($zids);
                                print("heeeeeeeeeelo");
                                
                                $itemarr['zid'] = $zids['zid'];
                                
                                $db->getTable("Zapisy", "INSERT");
				$db->setFields("pesel, zid, platnosc_id");
				$values = array($itemarr['pesel'], $itemarr['zid'], $itemarr['platnosc_id']);
				$db->setValues(implode(",", $values));
				$num = $db->execute();
				
				if($db->errno) // duplicate entry
				{          
					$this->view->assign("warning", "Error podczas insertu2");
					$this->view->assign("dump1", $itemarr);
				}
				if($num == 1)
				{         
					$this->view->assign("complete1", $num);
					
				}
			}
			elseif(!empty($itemarr))# && x == 4)
			{
                            
				$this->view->assign("errors", $err);
				$this->view->assign("dump", $itemarr);
                                $this->view->assign("dump1", $itemarr);
			}
		}
	}
 }
?>?>